package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.emailAccount.entity.SysMailAccountVO;
import cn.xenosp.server.module.emailAccount.service.SysMailAccountService;
import cn.xenosp.server.module.emailLog.entity.SysMailLogVO;
import cn.xenosp.server.module.emailLog.service.SysMailLogService;
import cn.xenosp.server.module.emailTemplate.entity.SysMailTemplateVO;
import cn.xenosp.server.module.emailTemplate.service.SysMailTemplateService;
import cn.xenosp.server.module.loginLog.entity.LoginLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2025-01-14 15:00
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/email/")
public class EmailController {
    @Autowired
    SysMailAccountService sysMailAccountService;
    @Autowired
    SysMailTemplateService sysMailTemplateService;
    @Autowired
    SysMailLogService sysMailLogService;

    @LogInfo(operationModuleType = "消息中心",operationName = "分页查询",operationContent = "查询邮箱账号数据")
    @PostMapping("/account/getPage")
    public Result<?> getAccountPage(@RequestBody String body){
        try {
            SysMailAccountVO sysMailAccountVO = ObjectConvert.urlStringToClass(body, SysMailAccountVO.class);
            return Result.OK("",sysMailAccountService.getPage(sysMailAccountVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "获取邮箱账号",operationContent = "获取邮箱账号")
    @PostMapping("/account/all")
    public Result<?> allAccount(){
        try {
            return Result.OK("",sysMailAccountService.getAll());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "新增邮箱账号",operationContent = "新增邮箱账号")
    @PostMapping("/account/add")
    public Result<?> addAccount(@RequestBody SysMailAccountVO  mailAccountVO){
        try {
            sysMailAccountService.add(mailAccountVO);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "修改邮箱账号",operationContent = "修改邮箱账号")
    @PostMapping("/account/save")
    public Result<?> saveAccount(@RequestBody SysMailAccountVO  mailAccountVO){
        try {
            sysMailAccountService.save(mailAccountVO);
            return Result.OK("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "删除邮箱账号",operationContent = "删除邮箱账号")
    @PostMapping("/account/del")
    public Result<?> delAccount(@RequestBody SysMailAccountVO  mailAccountVO){
        try {
            if (mailAccountVO.getId().contains(",")){
                sysMailAccountService.delete(mailAccountVO.getId().split(","));
            }else {
                sysMailAccountService.delete(mailAccountVO.getId());
            }
            return Result.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "分页查询",operationContent = "查询邮箱模板数据")
    @PostMapping("/template/getPage")
    public Result<?> getTemplatePage(@RequestBody String body){
        try {
            SysMailTemplateVO mailTemplateVO = ObjectConvert.urlStringToClass(body, SysMailTemplateVO.class);
            return Result.OK("",sysMailTemplateService.getPage(mailTemplateVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "新增邮箱模板",operationContent = "新增邮箱模板")
    @PostMapping("/template/add")
    public Result<?> addTemplate(@RequestBody SysMailTemplateVO  mailTemplateVO){
        try {
            sysMailTemplateService.add(mailTemplateVO);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "修改邮箱模板",operationContent = "修改邮箱模板")
    @PostMapping("/template/save")
    public Result<?> saveTemplate(@RequestBody SysMailTemplateVO  mailTemplateVO){
        try {
            sysMailTemplateService.save(mailTemplateVO);
            return Result.OK("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "删除邮箱模板",operationContent = "删除邮箱模板")
    @PostMapping("/template/del")
    public Result<?> delTemplate(@RequestBody SysMailTemplateVO  mailTemplateVO){
        try {
            if (mailTemplateVO.getId().contains(",")){
                sysMailTemplateService.delete(mailTemplateVO.getId().split(","));
            }else {
                sysMailTemplateService.delete(mailTemplateVO.getId());
            }
            return Result.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "分页查询",operationContent = "查询邮箱发送日志数据")
    @PostMapping("/log/getPage")
    public Result<?> getLogPage(@RequestBody String body){
        try {
            SysMailLogVO sysMailLogVO = ObjectConvert.urlStringToClass(body, SysMailLogVO.class);
            return Result.OK("",sysMailLogService.getPage(sysMailLogVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @LogInfo(operationModuleType = "消息中心",operationName = "删除邮箱发送日志",operationContent = "删除邮箱发送日志")
    @PostMapping("/log/del")
    public Result<?> delLog(@RequestBody SysMailAccountVO  mailAccountVO){
        try {
            if (mailAccountVO.getId().contains(",")){
                sysMailAccountService.delete(mailAccountVO.getId().split(","));
            }else {
                sysMailAccountService.delete(mailAccountVO.getId());
            }
            return Result.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
}
